Load in libraries

Set working directory. Replace with where your analysis folder is

setwd('/Users/kushin/Documents/Github/semantic_spaces/analysis')

Read in data and do some early preprocessing

d<-read.csv('../data/semantic_spaces_raw.csv') ## Read in raw dataset

d1<-d%>%filter(trial_type=='image-slider-responseMAS')

d1$trial_index<- as.numeric(d1$trial_index)
d1$prompt<- as.character(d1$prompt)
ratings_df <- d1%>%select(trial_index,rt,response, prompt,concept,color_index,subject_id, workerID, color_rgb)


uw58<-read.csv('../data/UW_58_rgb.csv', header = F) ## Read in color dataset
ratings_df$color_index<- ratings_df$color_index+1

ratings_df$response<-(ratings_df$response-min(ratings_df$response))/(max(ratings_df$response)-min(ratings_df$response))


## Remove problematic subjects
ratings_df<- ratings_df%>%filter(subject_id != 'nllbusbr3mkfsrmrozv1')
ratings_df<- ratings_df%>%filter(subject_id!= 'e70koc6vf0tnpg0f4bvz')
ratings_df%>%group_by(concept)%>%summarize(num_subs = length(unique(subject_id)) )

Replicate Tim’s initial analyses and visualizations

ratings_df_fruits<-  ratings_df%>%filter(concept=='fruits') ### subset dataset to only contain fruits
get.cor.mat <- function (df = ratings_df_fruits) 
{
#This function computes, for each subject and word, the
#correlation between the subject's ratings across colors
#and the mean of all other subject ratings across colors
#d is a 3D array containing the individual subject ratings
#formatted as in the original experiment
##########################

    nsj <- length(unique(df$subject_id))#Number of subjects
    nw <- 5  #Number of words
    subjects<- unique(df$subject_id)
    words<- unique(df$prompt)
    o <- matrix(0, nw, nsj)  #Initialize output matrix
    
    
    #Loop over words and subjects to compute each correlation and
    #store in output matrix
    for (i in c(1:nw)) for (j in c(1:nsj)) {
        s <- df%>%filter(subject_id == subjects [j],prompt==words[i])%>%arrange(color_index)%>%select(response)
        m <- df%>%filter(subject_id != subjects [j],prompt==words[i])%>%group_by(color_index)%>%summarize(mean_response = mean(response))
        o[i, j] <- cor(s$response, m$mean_response)
    }
    o #Return output matrix
}

words<-unique(ratings_df_fruits$prompt)
plot.cor.mat <- function (d) 
{
#This function plots the correlations of individual subject ratings
#with mean subject ratings for each word.
#d is a matrix output by get.cor.matrix
################################

    nw <- dim(d)[1] #Number of words
    nsj <- dim(d)[2] #Number of subjects
      
    
    #Generate blank plot
    plot(0, 0, type = "n", ylim = range(d), xlim = c(1, nw), 
        xlab = "Word", xaxt = "n",ylab = "Correlation")
    
    axis(1, at=seq(nw), labels=words)
        
    #Add individual subjects plotted as gray lines/dots
    for (i in c(1:nsj)) points(c(1:nw), d[, i], col = gray(0.8), 
        type = "o", pch = 16)
        
    #Add mean across subjects for each word as large black dot
    points(c(1:nw), rowMeans(d), pch = 16, cex = 2, col = 1)
    
    abline(h = 0) #Add horizontal line at r=0
}


test.complete <- function (d, p = 0.1, r = 4, plt = T, w = fruits, jc = judgedcols) 
{
#This function does matrix completion and plots results for
#a random hold-out set
#d: word-by-color matrix containing mean ratings
#p: Proportion of cells to use for hold-outs
#r: Maximum rank of matrix
#plt: Flag, should results be plotted?
#w: Character vector containing words
###########################################

    wm <- matrix(w, dim(d)[1], dim(d)[2]) #Matrix indicating word in each cell, for plotting later
    jcm <- t(matrix(jc, dim(d)[2], dim(d)[1])) #Matrix indicating color for each cell, for plotting later
    trn <- d #Make training matrix
    ncells <- dim(d)[1] * dim(d)[2] #How many cells in matrix?
    trn[sample(c(1:ncells), floor(p * ncells))] <- NA  #Remove hold-outs from training matrix
    m <- softImpute(trn, type = "als", rank.max = r) #Fit model
    pred <- complete(trn, m) #Make predictions for held-out items
    
    #Generate plot
    if (plt) {
        plot(d[!is.na(trn)], pred[!is.na(trn)], xlab = "True", 
            ylab = "Predicted", pch = 16, col = gray(0.8), xlim = range(d), 
            ylim = range(pred))
        points(d[is.na(trn)], pred[is.na(trn)], pch = 16,
               col=jcm[is.na(trn)])
        text(d[is.na(trn)], pred[is.na(trn)], label = wm[is.na(trn)], 
            cex = 0.7, col=jcm[is.na(trn)])
    }
    cor(d[is.na(trn)], pred[is.na(trn)])
}

Generate correlation and association rating plots.

There are error bar versions of the association ratings later in the notebook

dmat<-get.cor.mat()

plot.cor.mat(dmat)

mean_ratings<- ratings_df_fruits%>%group_by(prompt,color_index)%>%summarize(mean_response =mean(response))

par(mfrow = c(4,3), mar = c(1,1,1,0))
for(i in c(1:5)){
    barplot(mean_ratings[mean_ratings$prompt==unique(mean_ratings$prompt)[i],]$mean_response, col=rgb(uw58[,1],uw58[,2],uw58[,3]), border = NA, ylim = c(0,1), yaxt = "n")
    box()
    title(unique(mean_ratings$prompt)[i])
}

ratings_df_dir<-  ratings_df%>%filter(concept=='directions')
mean_ratings<-ratings_df_dir%>%group_by(prompt,color_index)%>%summarize(mean_response =mean(response))
sd(mean_ratings$mean_response)
## [1] 0.05023631
dmat<-get.cor.mat(ratings_df_dir)
words<-unique(ratings_df_dir$prompt)
plot.cor.mat(dmat)

par(mfrow = c(4,3), mar = c(1,1,1,0))
for(i in c(1:5)){
    barplot(mean_ratings[mean_ratings$prompt==unique(mean_ratings$prompt)[i],]$mean_response, col=rgb(uw58[,1],uw58[,2],uw58[,3]), border = NA, ylim = c(0,1), yaxt = "n")
    box()
    title(unique(mean_ratings$prompt)[i])
}

ratings_df_tod<-  ratings_df%>%filter(concept=='times-of-day')

mean_ratings<-ratings_df_tod%>%group_by(prompt,color_index)%>%summarize(mean_response =mean(response))

sd(mean_ratings$mean_response)
## [1] 0.09039403
dmat<-get.cor.mat(ratings_df_tod)

words<-unique(ratings_df_tod$prompt)
plot.cor.mat(dmat)

par(mfrow = c(4,3), mar = c(1,1,1,0))
for(i in c(1:5)){
    barplot(mean_ratings[mean_ratings$prompt==unique(mean_ratings$prompt)[i],]$mean_response, col=rgb(uw58[,1],uw58[,2],uw58[,3]), border = NA, ylim = c(0,1), yaxt = "n")
    box()
    title(unique(mean_ratings$prompt)[i])
}

ratings_df_emo<-  ratings_df%>%filter(concept=='emotions')
mean_ratings<-ratings_df_emo%>%group_by(prompt,color_index)%>%summarize(mean_response =mean(response))
sd(mean_ratings$mean_response)
## [1] 0.09233083
dmat<-get.cor.mat(ratings_df_emo)

words<-unique(ratings_df_emo$prompt)
plot.cor.mat(dmat)

par(mfrow = c(4,3), mar = c(1,1,1,0))
for(i in c(1:5)){
    barplot(mean_ratings[mean_ratings$prompt==unique(mean_ratings$prompt)[i],]$mean_response, col=rgb(uw58[,1],uw58[,2],uw58[,3]), border = NA, ylim = c(0,1), yaxt = "n")
    box()
    title(unique(mean_ratings$prompt)[i])
}

ratings_df_sc<-  ratings_df%>%filter(concept=='sceneries')
mean_ratings<-ratings_df_sc%>%group_by(prompt,color_index)%>%summarize(mean_response =mean(response))
sd(mean_ratings$mean_response)
## [1] 0.1335213
dmat<-get.cor.mat(ratings_df_sc)
words<-unique(ratings_df_tod$prompt)
plot.cor.mat(dmat)

par(mfrow = c(4,3), mar = c(1,1,1,0))
for(i in c(1:5)){
    barplot(mean_ratings[mean_ratings$prompt==unique(mean_ratings$prompt)[i],]$mean_response, col=rgb(uw58[,1],uw58[,2],uw58[,3]), border = NA, ylim = c(0,1), yaxt = "n")
    box()
    title(unique(mean_ratings$prompt)[i])
}

ratings_df_cl<-  ratings_df%>%filter(concept=='clothing')
ratings_df_cl<- ratings_df_cl%>%filter(subject_id!= 'e70koc6vf0tnpg0f4bvz')
mean_ratings<-ratings_df_cl%>%group_by(prompt,color_index)%>%summarize(mean_response =mean(response))




dmat<-get.cor.mat(ratings_df_cl)
words<-unique(ratings_df_tod$prompt)
plot.cor.mat(dmat)

par(mfrow = c(4,3), mar = c(1,1,1,0))
for(i in c(1:5)){
    barplot(mean_ratings[mean_ratings$prompt==unique(mean_ratings$prompt)[i],]$mean_response, col=rgb(uw58[,1],uw58[,2],uw58[,3]), border = NA, ylim = c(0,1), yaxt = "n")
    box()
    title(unique(mean_ratings$prompt)[i])
}

cor_mat <- matrix(0,5,58)
unique(mean_ratings$prompt)
## [1] "dress" "pants" "shirt" "shoes" "socks"
for(i in 1:5){
  for(j in 1:58){
  cor_mat[i,j] <-mean_ratings[mean_ratings$prompt==unique(mean_ratings$prompt)[i],]$mean_response[j]
  }
  }

dim(cor_mat)
## [1]  5 58
e_vals <- eigen(cor(t(cor_mat)))
barplot(e_vals$values)

Predictions from Matrix completion vs. true ratings for a single concept-category.

test.complete(cor_mat,w = unique(mean_ratings$prompt),jc=rgb(uw58[,1],uw58[,2],uw58[,3]), r=2)

## [1] 0.6015159

Interlude: Writing out a ratings csv for future analyses if needed.

really_run=F
if( really_run==T){
ratings_df$color_rgb<- paste0("#", ratings_df$color_rgb)

ratings_df$color_rgb<-ifelse(ratings_df$color_index==23, '#000000',ratings_df$color_rgb)

colnames(ratings_df)[which(names(ratings_df) == "color_rgb")] <- "color_hex"
colnames(ratings_df)[which(names(ratings_df) == "concept")] <- "category"
colnames(ratings_df)[which(names(ratings_df) == "prompt")] <- "concept"

write.csv(ratings_df,'../data/pilot0_ratings.csv')
}

Predictions from Matrix completion vs. true ratings for all concept-categories.

mean_ratings<- ratings_df%>%group_by(prompt,color_index)%>%summarize(mean_response =mean(response))


cor_mat <- matrix(0,30,58)
unique(mean_ratings$prompt)
##  [1] "above"      "angry"      "beach"      "below"      "beside"    
##  [6] "blueberry"  "dawn"       "day"        "disgust"    "dress"     
## [11] "dusk"       "far"        "fearful"    "field"      "happy"     
## [16] "lemon"      "mango"      "near"       "night"      "noon"      
## [21] "ocean"      "pants"      "sad"        "shirt"      "shoes"     
## [26] "sky"        "socks"      "strawberry" "sunset"     "watermelon"
for(i in 1:30){
  for(j in 1:58){
  cor_mat[i,j] <-mean_ratings[mean_ratings$prompt==unique(mean_ratings$prompt)[i],]$mean_response[j]
  }
  }


e_vals <- eigen(cor(t(cor_mat)))
barplot(e_vals$values)

test.complete(cor_mat,w = unique(mean_ratings$prompt),jc=rgb(uw58[,1],uw58[,2],uw58[,3]), r=)

## [1] 0.836388

Validating mturk data with lab data

Read in lab data for fruits and see how well they line up with mturk data.

We are going to compute the average concept x color association and first find the correlation between the entire lab dataset and entire mturk dataset.

frnames <- c("Mango", "Watermelon","Honeydew", "Cantaloupe", "Grapefruit", 
             "Strawberry", "Raspberry", "Blueberry", "Avocado", "Orange", "Lime", "Lemon")

dLab<- readMat('../data/FruitData.mat')

dLab<-dLab$FruitAssoc[,c(8,12,1,6,2),]


inds<-sample(54,54/2, replace = F)  ### sample half the participants in the lab data
inv_inds<-seq(54)[!seq(54) %in% inds] ### get indices for the other half of the participants
d1<-dLab[,,inds]
d2<-dLab[,,inv_inds]

mrating <- apply(dLab, c(1,2), "mean")

mrating
##             [,1]       [,2]       [,3]       [,4]       [,5]
##  [1,] 0.51222222 0.12087963 0.11143519 0.10078704 0.09231481
##  [2,] 0.80805556 0.07518519 0.07620370 0.08226852 0.06796296
##  [3,] 0.59643519 0.08402778 0.10768519 0.09490741 0.12981481
##  [4,] 0.39791667 0.12717593 0.10828704 0.10250000 0.11287037
##  [5,] 0.27824074 0.07629630 0.10231481 0.10083333 0.10152778
##  [6,] 0.21750000 0.10717593 0.11527778 0.21810185 0.18523148
##  [7,] 0.12902778 0.12032407 0.10888889 0.26981481 0.21208333
##  [8,] 0.17972222 0.08069444 0.11143519 0.31870370 0.25268519
##  [9,] 0.13601852 0.08953704 0.16000000 0.35513889 0.38337963
## [10,] 0.10856481 0.09064815 0.14861111 0.41722222 0.40097222
## [11,] 0.69712963 0.09708333 0.10356481 0.06958333 0.07629630
## [12,] 0.86166667 0.09083333 0.09555556 0.06296296 0.07564815
## [13,] 0.41861111 0.09037037 0.08027778 0.10587963 0.08953704
## [14,] 0.28347222 0.09125000 0.06824074 0.13361111 0.08884259
## [15,] 0.18583333 0.10490741 0.14476852 0.26074074 0.20194444
## [16,] 0.14310185 0.09236111 0.12481481 0.32763889 0.28314815
## [17,] 0.73782407 0.09597222 0.09277778 0.07407407 0.08810185
## [18,] 0.85152778 0.08745370 0.08236111 0.06509259 0.10134259
## [19,] 0.41115741 0.07935185 0.09356481 0.10439815 0.11800926
## [20,] 0.19004630 0.08402778 0.09319444 0.21851852 0.14439815
## [21,] 0.32092593 0.07935185 0.11439815 0.09375000 0.15041667
## [22,] 0.31092593 0.11125000 0.11009259 0.12037037 0.11699074
## [23,] 0.11185185 0.05569444 0.05060185 0.11013889 0.14324074
## [24,] 0.12615741 0.06865741 0.06629630 0.08287037 0.10101852
## [25,] 0.10708333 0.10884259 0.07680556 0.07560185 0.08916667
## [26,] 0.11337963 0.15236111 0.14333333 0.09425926 0.16060185
## [27,] 0.12199074 0.15703704 0.10013889 0.09347222 0.14606481
## [28,] 0.16888889 0.08009259 0.11217593 0.45023148 0.32652778
## [29,] 0.12509259 0.10675926 0.22259259 0.47069444 0.51625000
## [30,] 0.11319444 0.13032407 0.20736111 0.39944444 0.38041667
## [31,] 0.12574074 0.08912037 0.18597222 0.58782407 0.63527778
## [32,] 0.12412037 0.08921296 0.15189815 0.57995370 0.52393519
## [33,] 0.13726852 0.15092593 0.17782407 0.35620370 0.72375000
## [34,] 0.13712963 0.21824074 0.25430556 0.29115741 0.53143519
## [35,] 0.13949074 0.25287037 0.16615741 0.18861111 0.31967593
## [36,] 0.18148148 0.15194444 0.15388889 0.15694444 0.27583333
## [37,] 0.14231481 0.15722222 0.15703704 0.11578704 0.18287037
## [38,] 0.10967593 0.32083333 0.27685185 0.11490741 0.14902778
## [39,] 0.12300926 0.37564815 0.33800926 0.13856481 0.15083333
## [40,] 0.08865741 0.15268519 0.27453704 0.15175926 0.12652778
## [41,] 0.10726852 0.19740741 0.54282407 0.21810185 0.20712963
## [42,] 0.10296296 0.19782407 0.42847222 0.24546296 0.26930556
## [43,] 0.08754630 0.14379630 0.41870370 0.47282407 0.52800926
## [44,] 0.08555556 0.10273148 0.17435185 0.75083333 0.74111111
## [45,] 0.11129630 0.26212963 0.27726852 0.29222222 0.51875000
## [46,] 0.11893519 0.41796296 0.27425926 0.19486111 0.36111111
## [47,] 0.12995370 0.22333333 0.22666667 0.32550926 0.63217593
## [48,] 0.15773148 0.21884259 0.22143519 0.25527778 0.53986111
## [49,] 0.10712963 0.53546296 0.38037037 0.09782407 0.13782407
## [50,] 0.11180556 0.63013889 0.53523148 0.11962963 0.11652778
## [51,] 0.11305556 0.26916667 0.75180556 0.11800926 0.09240741
## [52,] 0.08694444 0.33268519 0.61810185 0.16365741 0.14337963
## [53,] 0.07500000 0.21726852 0.67023148 0.17569444 0.12893519
## [54,] 0.06532407 0.14365741 0.31583333 0.74569444 0.68097222
## [55,] 0.09791667 0.60143519 0.26208333 0.15152778 0.26893519
## [56,] 0.11439815 0.25310185 0.19717593 0.25625000 0.51129630
## [57,] 0.09675926 0.89444444 0.59981481 0.10828704 0.11226852
## [58,] 0.07263889 0.46495370 0.85421296 0.10856481 0.11513889
mean_fruit_ratings<- ratings_df_fruits%>%group_by(prompt,color_index)%>%summarize(mean_response =mean(response))



fruit_ratings_mat <- matrix(0,5,58)
unique(mean_fruit_ratings$prompt)
## [1] "blueberry"  "lemon"      "mango"      "strawberry" "watermelon"
for(i in 1:5){
  for(j in 1:58){
  fruit_ratings_mat[i,j] <-mean_fruit_ratings[mean_fruit_ratings$prompt==unique(mean_fruit_ratings$prompt)[i],]$mean_response[j]
  }
  }

dim(cor_mat)
## [1] 30 58
e_vals <- eigen(cor(t(fruit_ratings_mat)))
barplot(e_vals$values)

cor(mrating,t(fruit_ratings_mat))  ### The diagonal correlation values tell us how well lab and mturk data align
##            [,1]       [,2]        [,3]       [,4]        [,5]
## [1,]  0.8589780 -0.2907231 -0.42411176 -0.3856302 -0.43996180
## [2,] -0.4833513  0.8491743  0.77939497 -0.1922901  0.09785423
## [3,] -0.5191508  0.4682028  0.91962483  0.1351276  0.15616391
## [4,] -0.3086146 -0.1558111  0.03863194  0.9358359  0.71774337
## [5,] -0.4127773  0.1661255  0.13475851  0.7365515  0.93374279

Let’s visualize these association ratings to be sure

par(mfrow = c(2,3), mar = c(1,1,1,0))
for(i in c(1:5)){
    barplot(mrating[,i], col=rgb(uw58[,1],uw58[,2],uw58[,3]), border = NA, ylim = c(0,1), yaxt = "n")
    box()
    title(unique(mean_ratings$prompt)[i])
}

#mean_ratings<- ratings_df_fruits%>%group_by(prompt,color_index)%>%summarize(mean_response =mean(response))

par(mfrow = c(2,3), mar = c(1,1,1,0))
for(i in c(1:5)){
    barplot(mean_fruit_ratings[mean_fruit_ratings$prompt==unique(mean_fruit_ratings$prompt)[i],]$mean_response, col=rgb(uw58[,1],uw58[,2],uw58[,3]), border = NA, ylim = c(0,1), yaxt = "n")
    box()
    title(unique(mean_ratings$prompt)[i])
}

##Load up the saved ratings
ratings_df<-read.csv('../data/pilot0_ratings.csv')


## change some things to chars
ratings_df$category<- as.character(ratings_df$category)   
ratings_df$color_hex<- as.character(ratings_df$color_hex) 

Raw Association Ratings with error bars

Changereally_run to True to run and save these plots in the plots directory.

color_dict = unique(ratings_df$color_hex)
names(color_dict) = unique(ratings_df$color_index)


### function to plot the association ratings for a given concept
plot_assoc_ratings<- function(qconcept){
ratings_df_m<-ratings_df%>%group_by(concept, color_index)%>%summarize(sd_rating = sd(response),mean_rating = mean(response), category = category[1], color_hex = color_hex[1], num_raters = n()) ## hacky way of getting category and color

d<-ratings_df_m%>%filter(category==qconcept)



p<-ggplot(d, aes(x =color_index, y=mean_rating, fill = as.character(color_index)))+
  geom_bar(stat='identity',colour="black",width = 1, show.legend = FALSE)+
geom_errorbar(aes(ymin = mean_rating-(sd_rating/sqrt(num_raters)), ymax = mean_rating+(sd_rating/sqrt(num_raters))), width=1)+
  facet_wrap(~concept)+ylim(0,1)+scale_fill_manual(values=color_dict) +theme_classic() + theme(axis.ticks.x=element_blank(),axis.text.x=element_blank(),axis.text=element_text(size=12),
        axis.title=element_text(size=14,face="bold"), strip.text = element_text(size=20,face="bold") )

print(p)
really_run=F
if(really_run==T){
ggsave(
  paste0('../plots/',qconcept,'.png'),
  plot = last_plot(),
  device = "png",
  scale = 1,
  width = 20,
  height = 15,
  dpi = 300,
  limitsize = TRUE
)
}
}

for(this_concept in unique(ratings_df$category)){
  plot_assoc_ratings(this_concept)
}

Split half correlations. See how well half of the lab dataset correlates with another half sampled 100 times. This gives us a baseline of what to expect in terms of consistency of ratings and gives us something to compare our mturk ratings to.

dLab<- readMat('../data/FruitData.mat')
dLab<-dLab$FruitAssoc[,c(8,12,1,6,2),]

rawdat<-read.csv('../data/semantic_spaces_raw.csv')
uw58full<-read.csv('../data/UW58_colors.csv')


labR<-{}
turkR<-{}

for(i in 1:100){



s1<-sample(unique(ratings_df$subject_id), length(unique(ratings_df$subject_id))/2, replace = FALSE)

d1<-ratings_df[ratings_df$subject_id %in% s1,]

d2<-ratings_df[!(ratings_df$subject_id %in% s1),]


mean_ratings_d1<- d1%>%group_by(category,concept,color_index)%>%summarize(mean_response =mean(response))%>%filter(category=="fruits")

mean_ratings_d2<- d2%>%group_by(category,concept,color_index)%>%summarize(mean_response =mean(response))%>%filter(category=="fruits")

r_cofTurk <- cor(mean_ratings_d1$mean_response, mean_ratings_d2$mean_response)

turkR<-c(turkR,r_cofTurk)



inds<-sample(54,54/2, replace = F)
inv_inds<-seq(54)[!seq(54) %in% inds]
d1<-dLab[,,inds]
d2<-dLab[,,inv_inds]

d1_m<- apply(d1, c(1,2), "mean")
dim(d1_m)<-NULL

d2_m<- apply(d2, c(1,2), "mean")
dim(d2_m)<-NULL
r_cofLab<-  cor(d1_m,d2_m)
labR<-c(labR,r_cofLab)

}

print('Mturk summary:')
## [1] "Mturk summary:"
mean(turkR)
## [1] 0.8872687
sd(turkR)
## [1] 0.01776354
print('Lab summary:')
## [1] "Lab summary:"
mean(labR)
## [1] 0.9654667
sd(labR)
## [1] 0.005518388

Saving out mat files for ‘Tree Plots’

Code for generating association *.mat files for generating our ‘treeplots’ that show how association ratings are distributed over LAB space.

No need to run if plots already exist

really_run =F

if(really_run==T){
lab<-matrix(c(uw58full$L,uw58full$a,uw58full$b),c(58,3))

uw58mat<-data.matrix(uw58, rownames.force = NA)


assoc_df_full<- mean_ratings%>%filter(category=='clothing')

assoc_mat<-{}
Name<-{}
for(this_concept in unique(assoc_df_full$concept)){
  
assoc_df<- assoc_df_full%>%filter(concept==this_concept)
assoc_mat<- cbind(assoc_mat,assoc_df$mean_z_rating_norm)
Name<- cbind(Name,this_concept)
}


writeMat('..data/mturkclothes.mat', rgb=uw58mat,lab=lab,assoc=assoc_mat,Name=Name)
}

PCA with embedded Treeplots

### Z-score within participants and concepts
ratings_df<- ratings_df%>%group_by(subject_id,concept)%>%mutate(z_rating = (response - mean(response))/sd(response))


### Find mean ratings and normalize to 0,1 range
mean_ratings<- ratings_df%>%group_by(category,concept,color_index)%>%summarize(mean_rating = mean(response), mean_z_rating = mean(z_rating))

mean_ratings<-mean_ratings%>%group_by(concept)%>%mutate(mean_rating_norm = (mean_rating-min(mean_rating))/(max(mean_rating)-min(mean_rating)), mean_z_rating_norm = (mean_z_rating-min(mean_z_rating))/(max(mean_z_rating)-min(mean_z_rating)) )

### Create concept X color matrix 

ratings_mat <- matrix(0,30,58)
unique(mean_ratings$concept)
##  [1] dress      pants      shirt      shoes      socks      above     
##  [7] below      beside     far        near       angry      disgust   
## [13] fearful    happy      sad        blueberry  lemon      mango     
## [19] strawberry watermelon beach      field      ocean      sky       
## [25] sunset     dawn       day        dusk       night      noon      
## 30 Levels: above angry beach below beside blueberry dawn day ... watermelon
for(i in 1:30){
  for(j in 1:58){
  ratings_mat[i,j] <-mean_ratings[mean_ratings$concept==unique(mean_ratings$concept)[i],]$mean_z_rating_norm[j]
  }
}

### Compute ratings matrix and correlation matrix

rownames(ratings_mat)<- unique(mean_ratings$concept)
ratings_mat
##                  [,1]       [,2]       [,3]       [,4]      [,5]      [,6]
## dress      0.41804313 0.59197912 0.54743605 0.52778105 0.5053774 0.5169672
## pants      0.30303586 0.79600735 0.52756758 0.44621585 0.2116041 0.1867259
## shirt      0.37849903 0.63261282 0.50552848 0.71115038 0.3836461 0.3699878
## shoes      0.26459529 0.52757008 0.38545318 0.35071406 0.2074004 0.1935427
## socks      0.23747135 0.31150367 0.39541909 0.47161406 0.2831568 0.2090667
## above      1.00000000 0.59650933 0.83646594 0.76767027 0.5147738 0.5196138
## below      0.16563930 0.50558302 0.47136550 0.33076698 0.8330497 0.3647121
## beside     0.85229632 0.50710588 0.86589553 0.86489687 0.4393541 0.7275996
## far        0.09491143 0.99345659 0.67601072 0.56224559 0.8966686 0.7309487
## near       0.90989663 0.50486593 0.59270403 0.37989494 0.3625739 0.4286300
## angry      0.12876040 0.57958130 0.38715711 0.13613094 0.7040261 0.4013774
## disgust    0.36842216 0.68564340 0.46379700 0.18553726 0.5284971 0.3973859
## fearful    0.11885164 0.39101537 0.23590651 0.22777968 0.3516672 0.4985257
## happy      0.81148010 0.42556237 0.55421806 0.74182676 0.4443038 0.7116700
## sad        0.00000000 1.00000000 0.67421278 0.32854220 0.9575951 0.3732102
## blueberry  0.58477133 0.88497137 0.75332988 0.48232025 0.7100866 0.4219571
## lemon      0.20134203 0.11531068 0.20017223 0.14545710 0.0000000 0.2040166
## mango      0.09197228 0.13278834 0.17467684 0.12867398 0.1969627 0.1506258
## strawberry 0.17045445 0.19170998 0.08618594 0.16099027 0.2023277 0.1797278
## watermelon 0.19563915 0.08305567 0.20002801 0.03764951 0.1122980 0.1313389
## beach      0.96680554 0.75890062 0.94624727 0.92159221 0.1427532 0.1767773
## field      0.16444772 0.16324614 0.18923273 0.27798523 0.0327194 0.1023825
## ocean      0.72614661 0.75898095 1.00000000 0.77308064 0.1689774 0.1554762
## sky        0.90528302 0.75743587 0.96700082 1.00000000 0.1814827 0.2607220
## sunset     0.26445811 0.46241462 0.33715946 0.44347812 0.2410552 0.3865279
## dawn       0.67600247 0.46689769 0.50503829 0.90097628 0.1700565 0.5726544
## day        0.89230795 0.33998726 0.51328567 1.00000000 0.1904168 0.3128681
## dusk       0.29594010 1.00000000 0.72160891 0.27500267 0.9882020 0.8592329
## night      0.17305389 0.65986625 0.43474434 0.27341182 0.5071631 0.3997684
## noon       0.98406562 0.39203866 0.45785560 1.00000000 0.2066153 0.2512594
##                  [,7]       [,8]       [,9]       [,10]      [,11]
## dress      0.51455407 0.59645921 0.62638875 0.467328730 0.54363881
## pants      0.27744641 0.24786478 0.19483661 0.114976952 0.48596182
## shirt      0.49039328 0.21220502 0.49669872 0.056709014 0.59948796
## shoes      0.12708135 0.17980658 0.08275046 0.084524414 0.37144835
## socks      0.31566821 0.14467149 0.18778562 0.095797636 0.28111363
## above      0.61055899 0.60233330 0.79136978 0.639181031 0.89125670
## below      0.37585549 0.73628112 0.43117604 0.034567699 0.45322899
## beside     0.77919909 0.79232662 0.22543061 0.208678035 0.67650926
## far        0.38311572 0.66573654 0.82870741 0.647702390 0.27159429
## near       0.75665212 0.48292010 0.35431150 0.545489693 0.91315334
## angry      0.16741996 0.61942429 0.42261974 0.405997937 0.22739779
## disgust    0.24817174 0.50919888 0.39279643 0.530734586 0.34044065
## fearful    0.14378464 0.60559780 0.35366635 0.482483157 0.36760254
## happy      0.89311129 0.53935035 0.77196347 0.705150881 0.70192363
## sad        0.19628349 0.46652952 0.02835737 0.231017571 0.37524008
## blueberry  0.31537401 0.58837127 0.38260610 0.272016341 0.75445314
## lemon      0.07853730 0.07614389 0.17155971 0.058615860 0.02205459
## mango      0.17041365 0.17472191 0.14341140 0.155808822 0.08340230
## strawberry 0.33704795 0.37126501 0.38874996 0.624174582 0.07310804
## watermelon 0.16787250 0.17986630 0.37050459 0.347019058 0.04144396
## beach      0.45218083 0.17312724 0.00000000 0.093280943 0.98529175
## field      0.17508341 0.04715016 0.18706776 0.019499852 0.04660269
## ocean      0.08394079 0.03224509 0.06038305 0.002077604 0.87566736
## sky        0.28851938 0.19715646 0.17405985 0.134876804 0.96004088
## sunset     0.24667591 0.33747689 0.32586857 0.367238998 0.22224659
## dawn       0.46899880 0.47916999 0.33306055 0.345756376 0.66660971
## day        0.53639332 0.27913868 0.52778316 0.419702728 0.88074227
## dusk       0.48339413 0.68914637 0.61161687 0.440945338 0.74727820
## night      0.25077541 0.33632016 0.05883999 0.139146907 0.21248769
## noon       0.55309413 0.28557540 0.43536438 0.252739409 0.78081393
##                 [,12]      [,13]      [,14]       [,15]      [,16]
## dress      0.70203602 0.49703789 0.61444041 0.561962375 0.48833092
## pants      0.55374330 0.28971594 0.14893906 0.176349408 0.06613556
## shirt      0.64139738 0.54062271 0.40922795 0.367329095 0.21818709
## shoes      0.33491296 0.18497189 0.03879823 0.077834858 0.04607213
## socks      0.41629002 0.26674826 0.12616478 0.184615833 0.10083598
## above      0.87505302 0.76201454 0.61882653 0.704934682 0.74173587
## below      0.50467177 0.03890535 0.62708108 0.308303731 0.11437486
## beside     0.75732283 0.94690397 0.83428124 0.234789821 0.46420071
## far        0.94386915 0.46795544 0.90152763 0.509635627 0.26844669
## near       0.86217089 1.00000000 0.46786356 0.885949942 0.67250331
## angry      0.50338538 0.32627034 0.50807924 0.287579744 0.35372625
## disgust    0.52702508 0.36294518 0.52659084 0.320380325 0.25876373
## fearful    0.65133317 0.28288986 0.49369616 0.430070785 0.39621409
## happy      0.60203940 0.68774216 0.57560029 0.864017716 0.76849192
## sad        0.77825372 0.27927772 0.56580436 0.324918150 0.01690402
## blueberry  1.00000000 0.68360601 0.78663237 0.663290549 0.40926135
## lemon      0.16144945 0.10372822 0.16593080 0.084138447 0.03922982
## mango      0.09911263 0.10860999 0.09587610 0.143837949 0.16478827
## strawberry 0.13673064 0.21687577 0.24235820 0.243032119 0.33956753
## watermelon 0.14426197 0.08879153 0.14103675 0.202795228 0.24791634
## beach      0.91899105 0.43702826 0.16632516 0.112870679 0.13585133
## field      0.09378969 0.02385629 0.11687310 0.002872978 0.15060726
## ocean      0.83031057 0.60048396 0.19905919 0.102239048 0.02620248
## sky        0.85630198 0.65805724 0.28258072 0.260156955 0.18403065
## sunset     0.30004788 0.33678988 0.21997318 0.211508267 0.22520526
## dawn       0.53253960 0.43621879 0.19522779 0.226398629 0.00000000
## day        0.41028069 0.55874069 0.15709840 0.271670119 0.30781752
## dusk       0.92078519 0.77813236 0.83736123 0.716706897 0.69681895
## night      0.61139686 0.30676374 0.33155505 0.179487158 0.15627253
## noon       0.38595472 0.28887946 0.07288270 0.330855717 0.44343136
##                 [,17]     [,18]      [,19]      [,20]      [,21]
## dress      0.62606727 0.5787829 0.49381369 0.55067254 0.48648445
## pants      0.54218394 0.5081712 0.31933259 0.03736524 0.28856416
## shirt      0.41933908 0.5418620 0.42643012 0.32943835 0.49949753
## shoes      0.30670039 0.3278016 0.14785764 0.16703260 0.19145664
## socks      0.21257791 0.3125764 0.31232227 0.16196172 0.26107510
## above      0.92070253 0.8561332 0.71762796 0.70849502 0.62174806
## below      0.00000000 0.5595799 0.16487091 0.31197832 0.76139896
## beside     0.73939992 0.6036640 0.39472704 0.59502864 0.72308791
## far        0.41021354 0.4678776 0.28339509 0.18009418 0.97634249
## near       0.74986111 0.8150553 0.85080548 0.73249676 0.34622704
## angry      0.30311656 0.5894813 0.27652506 0.34278919 0.35161365
## disgust    0.32423089 0.4261006 0.37928886 0.33169215 0.42287928
## fearful    0.23831521 0.4464757 0.38402167 0.28647800 0.37621252
## happy      0.79157686 0.7984596 0.80514242 0.85968242 0.51619855
## sad        0.32142728 0.4803950 0.61077646 0.13423064 0.61321827
## blueberry  0.75845479 0.8384737 0.69821430 0.52367435 0.47995802
## lemon      0.13910711 0.0997666 0.16683144 0.09415308 0.27367962
## mango      0.13366406 0.1731669 0.09801027 0.15417937 0.13686664
## strawberry 0.12662165 0.1298164 0.22210917 0.26355034 0.09793142
## watermelon 0.04025879 0.1465435 0.09974225 0.12628696 0.28451341
## beach      0.97487821 0.8299790 0.27247573 0.01599171 0.65484195
## field      0.04437690 0.1876652 0.10327876 0.08140563 0.41419215
## ocean      0.78043668 0.7413499 0.54676227 0.07224203 0.58724994
## sky        0.94431870 0.8440238 0.58259660 0.31994234 0.46732068
## sunset     0.28908248 0.3693917 0.37173708 0.24900703 0.22505332
## dawn       0.84270752 0.6273083 0.47980140 0.47040373 0.41538714
## day        0.88210237 0.4957378 0.43435172 0.28660964 0.64376395
## dusk       0.61803334 0.8170741 0.77262320 0.35674592 0.57847315
## night      0.26645026 0.5590331 0.43942882 0.25354269 0.45043721
## noon       0.84135145 0.5639079 0.44798586 0.29326109 0.41758702
##                  [,22]      [,23]      [,24]     [,25]      [,26]
## dress      0.352128959 1.00000000 0.75638702 0.5870645 0.54057187
## pants      0.229508651 1.00000000 0.85765297 0.8017066 0.84246993
## shirt      0.325850630 1.00000000 0.78919231 0.6953360 0.79295322
## shoes      0.241832273 1.00000000 0.83926727 0.7090447 0.63690913
## socks      0.198292624 0.93998851 0.81562236 0.8112736 0.80496983
## above      0.987727111 0.61937271 0.00000000 0.3947538 0.42861201
## below      0.004655107 0.34246920 0.59169999 0.5380759 0.08992089
## beside     0.760278674 0.68081983 0.46631805 0.4380076 1.00000000
## far        0.513345695 0.53610034 0.79333372 1.0000000 0.45295781
## near       0.777667931 0.25858034 0.17816613 0.0000000 0.51191726
## angry      0.365581102 0.51963286 0.65452734 0.5572506 0.14856891
## disgust    0.134650117 0.66803711 0.92767800 0.4492983 0.28686849
## fearful    0.227605768 1.00000000 0.81197547 0.6055707 0.18103898
## happy      0.857151029 0.00000000 0.11749961 0.4332505 0.75697183
## sad        0.212366596 0.81673126 0.91413985 0.8906799 0.44985918
## blueberry  0.433155380 0.15418972 0.26941261 0.1046693 0.15202059
## lemon      0.179179264 0.10802660 0.08807439 0.1059142 0.14030121
## mango      0.138682991 0.04096606 0.15525094 0.0000000 0.13619147
## strawberry 0.076903051 0.04281089 0.00000000 0.1020577 0.19436417
## watermelon 0.187569416 0.00000000 0.13700273 0.1632412 0.04746592
## beach      0.600388092 0.13716384 0.33465646 0.4210066 0.72490609
## field      0.145502022 0.00000000 0.07470616 0.1849235 0.15750042
## ocean      0.752229493 0.24956852 0.27040853 0.2371830 0.27660592
## sky        0.694978636 0.50712685 0.49653187 0.5107433 0.55899095
## sunset     0.290574452 0.16351729 0.34489623 0.3072648 0.16334049
## dawn       0.436422846 0.11803107 0.13343219 0.2763664 0.84721577
## day        0.602218251 0.00000000 0.03924352 0.2151945 0.92330247
## dusk       0.269200126 0.79124489 0.90548292 0.7483332 0.74828378
## night      0.139667543 0.99124567 1.00000000 0.7082178 0.33629708
## noon       0.886047677 0.00000000 0.07869699 0.1316794 0.57747582
##                 [,27]      [,28]       [,29]      [,30]      [,31]
## dress      0.96929061 0.34402647 0.394416081 0.55308944 0.52539747
## pants      0.86390541 0.31183381 0.332614213 0.23163719 0.09196036
## shirt      0.95019991 0.38858952 0.247950812 0.55390662 0.29205394
## shoes      0.85163304 0.27643731 0.146322553 0.13226002 0.19453973
## socks      1.00000000 0.31875138 0.180879131 0.41856044 0.13904611
## above      0.82564059 0.44972082 0.334781268 0.67274312 0.81786836
## below      0.13420672 0.48815611 0.241066674 0.36940299 0.36014002
## beside     0.85050268 0.34907618 0.835950346 0.83704668 0.33198658
## far        0.15801797 0.95044232 0.816930440 0.32929277 0.70272464
## near       0.78366486 0.35274915 0.442257750 0.65211926 0.43085730
## angry      0.00000000 0.74042208 0.367397463 0.22926348 0.38608422
## disgust    0.00000000 0.74393849 0.606435920 0.26696799 0.58705318
## fearful    0.00000000 0.63828814 0.318770436 0.10182157 0.40708095
## happy      0.94563870 0.37630072 0.455634708 0.84788349 0.73186005
## sad        0.10594535 0.91503540 0.554729764 0.19564797 0.16874893
## blueberry  0.00000000 0.33456095 0.267401586 0.15960552 0.23426224
## lemon      0.24960281 0.07397897 0.006431587 0.11078705 0.17720674
## mango      0.08583280 0.18529784 0.222838528 0.28902983 0.23358242
## strawberry 0.23825756 0.52276605 0.514870285 0.49750352 0.75880913
## watermelon 0.12217271 0.36769426 0.417776067 0.44650729 0.63587987
## beach      0.67576096 0.16234246 0.190058627 0.43729414 0.23549736
## field      0.13077436 0.15007717 0.072273343 0.10093682 0.25480929
## ocean      0.35324378 0.00000000 0.012023660 0.03349333 0.09031170
## sky        0.64408365 0.19593158 0.121739253 0.33292817 0.22886295
## sunset     0.17661142 0.33155061 0.482967462 0.37603422 0.52215808
## dawn       0.47848201 0.28437786 0.558589510 0.59446153 0.37730940
## day        0.97751138 0.22974178 0.312952448 0.64034000 0.58252087
## dusk       0.49913809 0.78935104 0.757605354 0.26037533 0.72379544
## night      0.09005674 0.54371739 0.316180504 0.07340192 0.22161386
## noon       0.95478978 0.17852814 0.221255975 0.64144819 0.41276798
##                   [,32]     [,33]     [,34]     [,35]      [,36]
## dress      0.4714781652 0.4825408 0.3148415 0.4378849 0.46227280
## pants      0.0661132488 0.3287773 0.2932350 0.2470760 0.09090690
## shirt      0.1423982744 0.3803993 0.3133039 0.3938422 0.14951622
## shoes      0.1935754202 0.2325880 0.1544045 0.1613605 0.09579607
## socks      0.0000000000 0.2659806 0.1954838 0.1723318 0.18489935
## above      0.7866158255 0.3044957 0.5320014 0.6460270 0.81023162
## below      0.1722980577 1.0000000 0.9121244 0.5696693 0.21456322
## beside     0.0000000000 0.5016056 0.6460949 0.5428809 0.79346265
## far        0.6014008142 0.7567529 0.6489382 0.9499655 0.60457936
## near       0.6206194062 0.2814282 0.2751293 0.4563821 0.50325990
## angry      0.4345679717 0.4952244 0.5416439 0.3211618 0.17385673
## disgust    0.3828098217 0.6384675 0.5367714 0.3633884 0.25190630
## fearful    0.4660443548 0.4075117 0.5328838 0.2818672 0.07882527
## happy      0.6464044701 0.6508081 0.6479442 0.7723951 0.84970630
## sad        0.1357310982 0.7110336 0.5560953 0.2231472 0.21902234
## blueberry  0.0947222507 0.1890492 0.1913491 0.2605785 0.28060375
## lemon      0.0784771630 0.4481331 0.4795232 0.4098708 0.30439800
## mango      0.1565517245 0.3529817 0.3618841 0.2143426 0.16081605
## strawberry 0.6292565887 0.2263256 0.2240257 0.2003546 0.21723457
## watermelon 0.6091812533 1.0000000 0.7408893 0.4554213 0.50192507
## beach      0.0678050745 0.3049812 0.2391365 0.4351795 0.25232824
## field      0.0661333924 1.0000000 0.7933169 0.6475659 0.47683777
## ocean      0.0002255728 0.1913067 0.1816730 0.3372572 0.31690733
## sky        0.1777857560 0.0000000 0.2032144 0.2986244 0.30316841
## sunset     0.4279774387 0.1258555 0.1682712 0.1746904 0.10977814
## dawn       0.3367478624 0.4746981 0.2810039 0.5418619 0.32712681
## day        0.4025851753 0.5225216 0.6125939 0.6579385 0.79931855
## dusk       0.4321216262 0.5342065 0.5709931 0.3503932 0.17650736
## night      0.0867187700 0.3295553 0.2197541 0.2272417 0.16886586
## noon       0.2875840170 0.2020201 0.4875134 0.6779107 0.68668716
##                 [,37]      [,38]     [,39]      [,40]     [,41]      [,42]
## dress      0.00000000 0.12191699 0.3516169 0.25266336 0.2753712 0.51778649
## pants      0.32950255 0.56697905 0.6408985 0.54800350 0.3637891 0.13613606
## shirt      0.11372533 0.22706171 0.6150348 0.10810305 0.1618885 0.36373970
## shoes      0.50224150 0.35927100 0.3958414 0.49707482 0.2925188 0.20351287
## socks      0.34897434 0.48956116 0.5418493 0.24848577 0.1627749 0.18870682
## above      0.37867878 0.29109476 0.5376462 0.36234915 0.4928469 0.70254191
## below      0.62008770 0.57189217 0.3869924 0.49873438 0.3862444 0.40457324
## beside     0.28667086 0.45106989 0.7873153 0.17865854 0.3673643 0.39335304
## far        0.94064974 0.33884052 0.5885121 0.99305965 0.5423698 0.33710223
## near       0.15062875 0.27747598 0.4535014 0.34592071 0.2065273 0.42853296
## angry      0.76611740 0.58316758 0.2697865 0.80881980 0.5755452 0.16109310
## disgust    1.00000000 0.77339603 0.2368916 0.77849898 0.7130956 0.49168845
## fearful    0.55742767 0.52433432 0.2480540 0.69238488 0.6283619 0.20479095
## happy      0.21806330 0.35132699 0.8137847 0.41537310 0.4492006 0.81286074
## sad        0.93184647 0.85322376 0.3336099 0.95946966 0.6550388 0.27568268
## blueberry  0.09509458 0.09540713 0.1811121 0.24405278 0.1149728 0.11861064
## lemon      0.25841894 0.27493630 0.3424257 0.16200886 0.2060348 0.16649604
## mango      0.16734023 0.24036902 0.3218628 0.23491683 0.4331359 0.30254084
## strawberry 0.08743585 0.13401828 0.1617852 0.27644762 0.4385074 0.36593270
## watermelon 0.07707197 0.15413430 0.1120997 0.20518364 0.2669167 0.42214156
## beach      0.26072162 0.67972198 1.0000000 0.24344503 0.5629612 0.63496938
## field      0.43555885 0.35052943 0.4847390 0.25925056 0.3765499 0.17547559
## ocean      0.15619062 0.03287841 0.2028081 0.20443445 0.1305478 0.06103846
## sky        0.22795160 0.18013478 0.3188428 0.09667128 0.1995261 0.31640171
## sunset     0.25043430 0.23975554 0.2338072 0.35322653 0.6623817 0.60235869
## dawn       0.15588184 0.33348046 0.6773423 0.48113608 0.7747232 0.66973487
## day        0.08423724 0.15357161 0.6230365 0.15065157 0.4067805 0.65711438
## dusk       0.69849727 0.72728075 0.4803553 0.77584274 0.8787476 0.81436362
## night      0.61788584 0.28425740 0.2085259 0.42973820 0.3026408 0.11608366
## noon       0.25456119 0.31046855 0.8227000 0.15444490 0.3255210 0.65453780
##                 [,43]      [,44]      [,45]      [,46]     [,47]
## dress      0.46621458 0.43967297 0.20600011 0.35069642 0.3539867
## pants      0.12587400 0.00000000 0.19600391 0.20754042 0.1833345
## shirt      0.22963536 0.00000000 0.11330483 0.36034816 0.3098152
## shoes      0.12508237 0.12520107 0.16681037 0.12909884 0.1087954
## socks      0.10954682 0.10426907 0.32240023 0.14334301 0.2543732
## above      0.59865316 0.84328757 0.46024284 0.55113252 0.5008871
## below      0.28516014 0.06572696 0.64381974 0.25401202 0.4653680
## beside     0.35656657 0.37443873 0.72197102 0.53679179 0.4639902
## far        0.79235112 0.63282395 0.49480516 0.06808444 0.5227580
## near       0.59183509 0.37819566 0.08537738 0.68674085 0.3494003
## angry      0.62504522 0.64816556 0.46245713 0.31601141 0.2277438
## disgust    0.37140223 0.60929322 0.67667149 0.36100055 0.4337548
## fearful    0.47141779 0.46079943 0.48463021 0.19422204 0.3157676
## happy      0.54173336 0.53908166 0.68429474 0.82400155 0.7011026
## sad        0.40725805 0.04683670 0.32792549 0.12400528 0.4223407
## blueberry  0.10040492 0.17249050 0.15705282 0.01157018 0.2030993
## lemon      0.12956756 0.09679895 0.59383434 0.55913511 0.4864707
## mango      0.49587316 0.25116211 0.43435845 0.36874797 0.4291391
## strawberry 0.71653110 0.72283739 0.31425219 0.21988716 0.2525353
## watermelon 0.72650003 0.80763753 0.64133577 0.49259492 0.8512476
## beach      0.23011959 0.14718871 0.31018762 0.33932285 0.3209098
## field      0.16220736 0.05698610 0.84574915 0.73234704 0.8332894
## ocean      0.06365599 0.02236404 0.21943047 0.09823402 0.1770980
## sky        0.26125794 0.11281805 0.05691592 0.18683833 0.1111707
## sunset     0.77050324 0.45801772 0.21353810 0.28500244 0.1354445
## dawn       0.58587507 0.43766971 0.39519911 0.32757702 0.3508018
## day        0.48690291 0.41039388 0.64006763 0.70828527 0.8640082
## dusk       0.59202887 0.42417807 0.30073076 0.00000000 0.4601102
## night      0.16694878 0.00000000 0.18826681 0.08567991 0.2506188
## noon       0.62390418 0.48582007 0.50660668 0.92361468 0.5559565
##                 [,48]       [,49]     [,50]      [,51]      [,52]
## dress      0.33214844 0.035334174 0.4694787 0.15750475 0.20420918
## pants      0.16975464 0.176888243 0.3488000 0.19341085 0.24816178
## shirt      0.14751898 0.009619596 0.3887097 0.14458562 0.16979652
## shoes      0.08802711 0.170230107 0.2049512 0.14395513 0.18076264
## socks      0.18580634 0.211128099 0.1897444 0.26641331 0.10544472
## above      0.75770176 0.481270228 0.8647061 0.55362428 0.80969600
## below      0.53174071 0.584291739 0.4242513 0.66366682 0.48799160
## beside     0.44431670 0.137217957 0.4931699 0.52760631 0.71128591
## far        0.29557321 0.302362519 0.2351677 0.39613701 0.52818420
## near       0.73899682 0.276839633 0.6916868 0.24548906 0.60503903
## angry      0.07895797 0.508607658 0.4309019 0.36674718 0.54884967
## disgust    0.35167694 0.752727802 0.5925559 0.77487342 0.31679256
## fearful    0.27002576 0.331252944 0.2747573 0.42425689 0.21493759
## happy      0.88957813 0.502075890 0.7433815 0.49183030 0.79110833
## sad        0.25751287 0.775315146 0.2568971 0.68882815 0.24182055
## blueberry  0.21829265 0.064465061 0.1450338 0.16695926 0.08991339
## lemon      0.53105689 0.580372535 0.7213943 0.31956063 0.35625694
## mango      0.36548014 0.558897787 0.6138869 0.70675899 0.75018934
## strawberry 0.28801099 0.218437154 0.1518077 0.24215008 0.21552925
## watermelon 0.77782258 0.237743294 0.2531597 0.24728684 0.16070898
## beach      0.24569969 0.312510111 0.5762885 0.51156887 0.83564935
## field      0.86797475 0.573985873 0.3954553 0.44439967 0.27347763
## ocean      0.18742017 0.078436863 0.1097481 0.03182947 0.06688580
## sky        0.11626299 0.164677855 0.3239630 0.21920778 0.33794042
## sunset     0.12000801 0.586157231 0.8100031 0.98682807 0.86836494
## dawn       0.56596580 0.458255105 0.8659611 0.71189167 1.00000000
## day        0.65568147 0.305948000 0.7284943 0.56209454 0.68342751
## dusk       0.18112372 0.812696294 0.3372293 0.73453251 0.59622187
## night      0.24205639 0.282428740 0.1494866 0.30873354 0.18169812
## noon       0.77385669 0.229213512 0.7980844 0.51876711 0.81066841
##                 [,53]      [,54]      [,55]      [,56]      [,57]
## dress      0.30731181 0.34539534 0.13137973 0.18741856 0.52653383
## pants      0.15931201 0.05581034 0.13262164 0.13452786 0.14516991
## shirt      0.17324492 0.30585587 0.06093022 0.02095262 0.13615945
## shoes      0.14552997 0.32806704 0.14452600 0.00000000 0.16473963
## socks      0.01767115 0.12781856 0.18974194 0.12950585 0.22278774
## above      0.63295245 0.83709328 0.68009515 0.64560922 0.87778511
## below      0.52126471 0.07587266 0.01578619 0.39536326 0.05894697
## beside     0.81715719 0.69421615 0.43047375 0.18889291 0.72719344
## far        0.75594655 0.33577137 0.43045738 0.07241093 0.00000000
## near       0.75783583 0.47175045 0.88774176 0.78448413 0.64409872
## angry      0.70358175 1.00000000 0.37508489 0.41805631 0.41174238
## disgust    0.62356466 0.41867416 0.53584233 0.28351369 0.51238365
## fearful    0.54924666 0.61812036 0.26045096 0.25638969 0.52287832
## happy      0.64671664 0.53925511 0.77111560 1.00000000 0.77059928
## sad        0.45411478 0.13846970 0.34339621 0.11320424 0.18911898
## blueberry  0.09695262 0.06868019 0.03777019 0.13025893 0.11404613
## lemon      0.10733962 0.16510818 0.86738426 0.61273730 1.00000000
## mango      0.63942932 0.41349431 0.62795551 0.43914041 0.91272257
## strawberry 0.47960623 1.00000000 0.18471865 0.22912195 0.13724249
## watermelon 0.39274945 0.86464925 0.42319255 0.73814485 0.34190861
## beach      0.45474810 0.15075040 0.28595385 0.01329782 0.64258284
## field      0.26575680 0.09400239 0.61665988 0.72109189 0.42480280
## ocean      0.07232809 0.04566049 0.06651055 0.05921115 0.11932237
## sky        0.25776523 0.19367365 0.12880172 0.15344750 0.26240634
## sunset     0.90722640 0.66446494 0.24924918 0.00000000 0.89296405
## dawn       0.77610173 0.36950097 0.41388820 0.30730019 0.80731929
## day        0.63222763 0.36030373 0.66157421 0.78346909 0.92556246
## dusk       0.60404264 0.39332245 0.18756601 0.29888918 0.55739244
## night      0.11174784 0.04884088 0.05572455 0.06181435 0.16460326
## noon       0.62558843 0.56847747 0.83459027 0.87223161 0.87072631
##                [,58]
## dress      0.2780581
## pants      0.1332884
## shirt      0.1364160
## shoes      0.1535112
## socks      0.2095149
## above      0.9409956
## below      0.2652649
## beside     0.8472292
## far        0.2115290
## near       0.7893113
## angry      0.4132912
## disgust    0.4392586
## fearful    0.4735929
## happy      0.8316211
## sad        0.2719759
## blueberry  0.2241610
## lemon      0.6979068
## mango      1.0000000
## strawberry 0.2126649
## watermelon 0.1991368
## beach      0.4090778
## field      0.3354827
## ocean      0.1393988
## sky        0.3328673
## sunset     1.0000000
## dawn       0.9242307
## day        0.9128991
## dusk       0.3131019
## night      0.1682407
## noon       0.9485232
dim(ratings_mat)
## [1] 30 58
cormat<-cor(t(ratings_mat), use= 'complete.obs')
covmat<-cov(t(ratings_mat), use= 'complete.obs')
dim(cormat)
## [1] 30 30
eigen(cormat)$values
##  [1] 8.81079447 7.15130815 3.22692214 2.50805679 1.94029491 1.14042177
##  [7] 0.93023420 0.80051131 0.60861556 0.40309129 0.38468787 0.35378535
## [13] 0.25436686 0.24346082 0.18469521 0.15014185 0.13915447 0.12906872
## [19] 0.11521781 0.09915838 0.08891633 0.07360007 0.06307285 0.04581133
## [25] 0.04092314 0.03631159 0.03241711 0.01922617 0.01550771 0.01022577
barplot(eigen(cormat)$values)

### Do the PCA
factors <- principal(r = cormat, nfactors = 5, rotate= 'varimax', covar = F, scores= T)

factors
## Principal Components Analysis
## Call: principal(r = cormat, nfactors = 5, rotate = "varimax", covar = F, 
##     scores = T)
## Standardized loadings (pattern matrix) based upon correlation matrix
##              RC1   RC5   RC2   RC3   RC4   h2    u2 com
## dress      -0.12  0.51  0.19 -0.54 -0.28 0.68 0.319 2.9
## pants       0.24  0.86  0.33 -0.01 -0.05 0.91 0.087 1.5
## shirt      -0.05  0.78  0.35 -0.25 -0.24 0.85 0.146 1.8
## shoes       0.29  0.89  0.09 -0.11  0.02 0.90 0.098 1.3
## socks       0.09  0.95  0.08  0.02 -0.12 0.94 0.063 1.1
## above      -0.63 -0.27  0.33 -0.34  0.22 0.74 0.256 2.9
## below       0.57 -0.06  0.14  0.51 -0.20 0.64 0.359 2.4
## beside     -0.33  0.32  0.44 -0.05  0.16 0.43 0.570 3.1
## far         0.67 -0.04  0.11 -0.09 -0.27 0.54 0.457 1.4
## near       -0.62 -0.23  0.36 -0.30  0.07 0.67 0.335 2.5
## angry       0.76 -0.19 -0.21 -0.11  0.22 0.72 0.284 1.5
## disgust     0.84 -0.05 -0.15  0.22  0.20 0.82 0.178 1.3
## fearful     0.78  0.10 -0.19 -0.16  0.10 0.69 0.312 1.3
## happy      -0.88 -0.29  0.09  0.07 -0.04 0.86 0.135 1.3
## sad         0.86  0.25  0.24  0.19 -0.04 0.89 0.108 1.4
## blueberry   0.11 -0.19  0.80 -0.36 -0.31 0.92 0.083 1.9
## lemon      -0.27 -0.09 -0.16  0.74  0.32 0.76 0.245 1.8
## mango      -0.10 -0.27 -0.25  0.44  0.75 0.89 0.107 2.2
## strawberry -0.04 -0.39 -0.60 -0.44  0.14 0.73 0.270 2.8
## watermelon -0.15 -0.39 -0.57  0.30 -0.10 0.60 0.398 2.6
## beach      -0.22  0.33  0.72  0.13  0.36 0.82 0.180 2.3
## field      -0.12 -0.14 -0.19  0.93 -0.06 0.93 0.070 1.2
## ocean      -0.12  0.19  0.88 -0.06 -0.11 0.83 0.166 1.2
## sky        -0.20  0.37  0.81 -0.27  0.04 0.90 0.096 1.8
## sunset      0.12 -0.19 -0.07 -0.15  0.90 0.89 0.108 1.2
## dawn       -0.35  0.02  0.34  0.10  0.72 0.77 0.231 2.0
## day        -0.80  0.03  0.21  0.29  0.28 0.85 0.147 1.7
## dusk        0.71  0.14  0.30 -0.32  0.00 0.71 0.290 1.9
## night       0.70  0.49  0.30 -0.04 -0.18 0.86 0.141 2.4
## noon       -0.81 -0.01  0.18  0.24  0.36 0.88 0.124 1.7
## 
##                        RC1  RC5  RC2  RC3  RC4
## SS loadings           7.90 4.75 4.68 3.35 2.96
## Proportion Var        0.26 0.16 0.16 0.11 0.10
## Cumulative Var        0.26 0.42 0.58 0.69 0.79
## Proportion Explained  0.33 0.20 0.20 0.14 0.13
## Cumulative Proportion 0.33 0.54 0.73 0.87 1.00
## 
## Mean item complexity =  1.9
## Test of the hypothesis that 5 components are sufficient.
## 
## The root mean square of the residuals (RMSR) is  0.05 
## 
## Fit based upon off diagonal values = 0.98

Proportion Variance explained - 0.26 0.16 0.16 0.11 0.10

factor_df<-factors$loadings
factor_df<- as.data.frame(unclass(factor_df))
factor_df<- cbind(factor_df, unique(mean_ratings$concept))
colnames(factor_df)<- c('PA1','PA2','PA5','PA3','PA4','concept')
factor_df

This will read in the treeplots generated through MATLAB and embed them in a plot where the position of the concept is determined by its first and second principal component loading.

p<- ggplot(factor_df, aes(x=PA1,y=PA2))+geom_point(alpha=0)+ ylim(c(-1,1))+xlim(c(-1,1))+ theme_classic()
for(this_concept in unique(factor_df$concept)){
pdsub<-factor_df[factor_df$concept==this_concept,]
g<- rasterGrob( readPNG(paste0('../plots/',this_concept,'.png')), interpolate=TRUE)
g$raster[g$raster=="#FFFFFF"] = "#FFFFFF00" 


p<-p+annotation_custom(g,xmin=pdsub$PA1-0.2, xmax=pdsub$PA1+0.2,ymin=pdsub$PA2-0.2, ymax=pdsub$PA2+0.2)

p<-p+ geom_text(pdsub, mapping= aes(label=concept, alpha=0.3), vjust=-3, show.legend=F)
}

print(p)

#ggsave(plot = p,filename = '../plots/test.png', height = 6.5, width = 6.5, dpi = 300)

Code to generate 1D PCA plots (Still in development. Hacky solutions so far.)

top4cols<-{}
for( i in 1:nrow(ratings_mat)){
  top4cols<-rbind(top4cols, order(ratings_mat[i,],decreasing=T)[1:4])

}

rownames(top4cols) <- rownames(factor_df)



top1cols<-top4cols[,1]

top2cols<-top4cols[,2]

top3cols<-top4cols[,3]

top4cols<-top4cols[,4]


concept_to_hex<-{}
for(i in 1:length(top1cols)){
  concept_to_hex<-append(concept_to_hex,color_dict[names(color_dict)==top1cols[i]])
}


concept_to_hex2<-{}
for(i in 1:length(top2cols)){
  concept_to_hex2<-append(concept_to_hex2,color_dict[names(color_dict)==top2cols[i]])
}

concept_to_hex3<-{}
for(i in 1:length(top3cols)){
  concept_to_hex3<-append(concept_to_hex3,color_dict[names(color_dict)==top3cols[i]])
}


concept_to_hex4<-{}
for(i in 1:length(top4cols)){
  concept_to_hex4<-append(concept_to_hex4,color_dict[names(color_dict)==top4cols[i]])
}

#concept_to_hex = list(concept_to_hex)
names(concept_to_hex)<- rownames(top4cols)
names(concept_to_hex2)<- rownames(top4cols)
names(concept_to_hex3)<- rownames(top4cols)
names(concept_to_hex4)<- rownames(top4cols)
ggplot(factor_df, aes(x=c(0), y=PA4, label= rownames(factor_df))) + geom_point(alpha=0)  + 
  geom_text( size=3.5)+ geom_point(aes(x=c(0.5),y=PA4,fill=rownames(factor_df)), shape=22,show.legend = FALSE, size=3.5,stroke = 0)+ xlim(-1,2.5)+scale_fill_manual(values=concept_to_hex4)+
  theme_classic()+
  theme(axis.title.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank())

#ggsave(filename = 'tempPC4_4.png', plot= last_plot(), width= 5, height = 13, dpi=300)

Matrix reconstruction using few principal components.

Writing out csv files for ease of use later.

really_run=F
if(really_run==T){

write.csv(ratings_mat, '..data/ratings_matrix.csv')
color_dict_df<- data.frame('hex' =color_dict, 'index' = names(color_dict))
color_dict_df
write.csv(color_dict_df, '..data/color_dict.csv')
}
if(!exists('ratings_mat')){
  ratings_mat = read.csv('../data/ratings_matrix.csv')
}

if(!exists('color_dict_df')){
  color_dict_df = read.csv('../data/color_dict.csv')
}

### Generate PCA object from conceptx color ratings matrix
pca2<-ratings_mat%>%prcomp(scale=F,center=F, retx = T)


### Function for reconstructing the matrix using fewer components
reverse_pca <- function(n_comp = 2, pca_object = pca2){
  
  ## Multiply the matrix of rotated data by the transpose of the matrix 
  ## of eigenvalues (i.e. the component loadings) to get back to a 
  ## matrix of original data values
  recon <- pca_object$x[, 1:n_comp] %*% t(pca_object$rotation[, 1:n_comp])
  
  ## Reverse any scaling and centering that was done by prcomp()
  
  if(all(pca_object$scale != FALSE)){
    ## Rescale by the reciprocal of the scaling factor, i.e. back to
    ## original range.
    recon <- scale(recon, center = FALSE, scale = 1/pca_object$scale)
  }
  if(all(pca_object$center != FALSE)){
    ## Remove any mean centering by adding the subtracted mean back in
    recon <- scale(recon, scale = FALSE, center = -1 * pca_object$center)
  }
  
  ## Make it a data frame that we can easily pivot to long format
  ## (because that's the format that the excellent imager library wants
  ## when drawing image plots with ggplot)
  recon_df <- data.frame(cbind(1:nrow(recon), recon))
  colnames(recon_df) <- c("x", 1:(ncol(recon_df)-1))

  ## Return the data to long form 
  recon_df_long <- recon_df %>%
    tidyr::pivot_longer(cols = -x, 
                        names_to = "y", 
                        values_to = "value") %>%
    mutate(y = as.numeric(y)) %>%
    arrange(y) %>%
    as.data.frame()
  
  recon_df_long
  return(recon_df)
}

a<-reverse_pca(n_comp = 11)

write.csv(a, 'ratings_matrix_11PC.csv')